package avicit.bdp.dms.prm.Integration;

import avicit.bdp.common.base.BaseDAO;
import avicit.bdp.common.utils.DateUtils;
import avicit.bdp.common.utils.SpringApplicationContext;
import avicit.bdp.dms.das.dao.BdpDasCategoryDao;
import avicit.bdp.dms.das.dto.BdpDasCategoryDTO;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 模型分类消息处理
 *
 * @author xugb
 * @date 2021/11/4 17:56
 */

public class TableTypeHandle extends BaseMessageHandle {
    private static final Logger log = LoggerFactory.getLogger(TableTypeHandle.class);

    private BdpDasCategoryDao dao;

    public static final String DEFAULT_TYPE_ID = "402891a4763fe3770176404c90f11164";

    public TableTypeHandle() {
        dao = SpringApplicationContext.getBean(BdpDasCategoryDao.class);
    }

    @Override
    public void excuteUpdate(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            BdpDasCategoryDTO dto = convertDto(map);
            dao.updateByPrimaryKey(dto);
        }
    }

    @Override
    public void excuteInsert(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            BdpDasCategoryDTO dto = convertDto(map);
            BdpDasCategoryDTO oldDto = dao.selectByPrimaryKey(dto.getId());
            if (oldDto != null) {
                dao.updateByPrimaryKey(dto);
            } else {
                dao.insert(dto);
            }

        }
    }

    private BdpDasCategoryDTO convertDto(Map<String, Object> map) {
        String id = MapUtils.getString(map, "id");
        String name = MapUtils.getString(map, "name");
        String parentId = MapUtils.getString(map, "parent_id");
        if(StringUtils.isBlank(parentId)){
            parentId = "0";
        }else {
            BdpDasCategoryDTO bdpDasCategoryDTO = dao.selectByPrimaryKey(parentId);
            if(bdpDasCategoryDTO == null){
                parentId = "0";
            }
        }
        Integer leaf = MapUtils.getInteger(map, "leaf");
        if("0".equals(id)){
            id = DEFAULT_TYPE_ID;
            leaf = 1;
        }
        String code = MapUtils.getString(map, "code");
        String remark = MapUtils.getString(map, "remark");
        Integer orderBy = MapUtils.getInteger(map, "order_by");
        String createdBy = MapUtils.getString(map, "created_by");
        if(StringUtils.isBlank(createdBy)) {
            createdBy = "1";
        }
        String creationDate = MapUtils.getString(map, "creation_date");
        String lastUpdatedBy = MapUtils.getString(map, "last_updated_by");
        String lastUpdateDate = MapUtils.getString(map, "last_update_date");
        BdpDasCategoryDTO dto = new BdpDasCategoryDTO();
        dto.setId(id);
        dto.setName(name);
        dto.setParentId(parentId);
        dto.setLeaf(1);
        dto.setCode(code);
        dto.setRemark(remark);
        dto.setOrderBy(orderBy);
        dto.setCreatedBy(createdBy);
        dto.setCreationDate(DateUtils.stringToDate(creationDate));
        dto.setLastUpdatedBy(lastUpdatedBy);
        dto.setLastUpdateDate(DateUtils.stringToDate(lastUpdateDate));
        return dto;
    }

    public BaseDAO getDAO() {
        return this.dao;
    }


    @Override
    public String getType() {
        return "tableCatalog";
    }

}
